package org.liquidByte.trafficMustang.androidApp.activities;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.App;
import org.liquidByte.trafficMustang.androidApp.LocationTracker;
import org.liquidByte.trafficMustang.androidApp.listview.adapter.SolicitarDeslocamentoRowAdapter;
import org.liquidByte.trafficMustang.androidApp.listview.row.SolicitarDeslocamentoRow;
import org.liquidByte.trafficMustang.androidApp.services.DeslocamentoService;
import org.liquidByte.trafficMustang.androidApp.services.RastreamentoService;
import org.liquidByte.trafficMustang.androidApp.services.Services;
import org.liquidByte.trafficMustang.androidApp.services.TrechoService;
import org.liquidByte.trafficMustang.androidApp.services.dto.DeslocamentoDTO;
import org.liquidByte.trafficMustang.androidApp.services.dto.LocalizacaoDTO;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class SolicitarDeslocamentoActivity extends Activity implements OnClickListener {
	
	private static final int REQUEST_CODE_VISUALIZAR_LOCALIZACAO = 0;
	
	private ListView listView;
	private ImageButton imgbLocalizacao;
	private ImageButton imgbCancelar;
	private SolicitarDeslocamentoRowAdapter adapter;
	private LocalizacaoDTO destino;
	
	protected ListView getListView() {
		if (this.listView == null) {
			this.listView = (ListView)this.findViewById(R.id.activity_solicitar_deslocamento_listview);
		}
		return listView;
	}

	protected ImageButton getImgbLocalizacao() {
		if (this.imgbLocalizacao == null) {
			this.imgbLocalizacao = (ImageButton)this.findViewById(R.id.activity_solicitar_deslocamento_localizacao);
		}
		return imgbLocalizacao;
	}
	
	protected SolicitarDeslocamentoRowAdapter getAdapter() {
		if (this.adapter == null) {
			this.adapter = new SolicitarDeslocamentoRowAdapter(this);
		}
		return adapter;
	}
	
	protected ImageButton getImgbCancelar() {
		if (this.imgbCancelar == null) {
			this.imgbCancelar = (ImageButton)this.findViewById(R.id.activity_solicitar_deslocamento_imgbCancelar);
		}
		return imgbCancelar;
	}

	protected LocalizacaoDTO getDestino() {
		return destino;
	}

	protected void setDestino(LocalizacaoDTO destino) {
		this.destino = destino;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_solicitar_deslocamento);
		
		this.getImgbLocalizacao().setOnClickListener(this);
		this.getImgbCancelar().setOnClickListener(this);
		this.getAdapter().setOnClickListener(this);
		
		this.getListView().setAdapter(this.getAdapter());
	}

	@Override
	public void onClick(View v) {
		if (this.getImgbLocalizacao() == v) {
			this.startLocalizacao();
		} else if (this.getImgbCancelar() == v) {
			this.cancel();
		} else if (v.getTag() != null && v.getTag() instanceof SolicitarDeslocamentoRow) {
			SolicitarDeslocamentoRow row = (SolicitarDeslocamentoRow)v.getTag();
			this.requisitarTrecho(row.getModel());
		}
	}
	
	private void cancel() {
		this.finish();
	}
	
	private void confirm() {
		this.setResult(Activity.RESULT_OK);
		this.finish();
	}
	
	private void requisitarTrecho(DeslocamentoDTO deslc) {
		TrechoService service = Services.trecho();
		RastreamentoService rastrService = Services.rastreamento();
		
		try {
			rastrService.update();
			service.requisitar(deslc);
			this.confirm();
		} catch (Exception e) {
			App.error(e);
		}
	}
	
	private void startLocalizacao() {
		Intent intent = new Intent(this, VisualizarLocalizacaoActivity.class);
		this.startActivityForResult(intent, REQUEST_CODE_VISUALIZAR_LOCALIZACAO);
	}
	
	private void refresh() {
		if (this.getDestino() != null) {
			DeslocamentoService deslcServ = Services.deslocamento();
			
			this.getAdapter().clear();
			
			try {
				DeslocamentoDTO[] deslcs = deslcServ.localizar(LocalizacaoDTO.fromLocation(LocationTracker.getInstance().getLastKnown()), this.getDestino());
				for (DeslocamentoDTO item : deslcs) {
					this.getAdapter().put(item);
				}
			} catch (Exception e) {
				App.error(e);
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		switch (requestCode) {
		case REQUEST_CODE_VISUALIZAR_LOCALIZACAO:
			if (resultCode == Activity.RESULT_OK) {
				LocalizacaoDTO localizacao = (LocalizacaoDTO)data.getSerializableExtra(LocalizacaoDTO.class.getName());
				this.setDestino(localizacao);
				this.refresh();
			}
			break;
		default:
			break;
		}
	}
}
